﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Psl.Chat;

namespace Stl.Chat.Model.Plugin
{
    /// <summary>
    /// 
    /// </summary>
    public class ChatModel : IChat
    {
        /************************************************************
         *                          Champs                          *
         ************************************************************/
        /// <summary>
        /// Un timbrage chronologique global de messages.
        /// </summary>
        protected int chrono = 0;

        /// <summary>
        /// List qui contient les messages mémorisés.
        /// </summary>
        protected List<ChatItem> messages = new List<ChatItem>();

        /// <summary>
        /// La partie préfixe de nom d'utilisateur.
        /// </summary>
        protected const String UserName = "user";

        /// <summary>
        /// Le nombre d'utilisteurs.
        /// </summary>
        protected int nbUser = 0;


        /************************************************************
         *                      Interface IChat                     *
         ************************************************************/
        #region IChat Members

        public void Add(string userID, string message)
        {
            chrono++;
            messages.Add(new ChatItem(Chrono, userID, message));
            FireChatChanged(this, new ChatChangedEventArgs(false, Chrono, Count));    // a revoir
        }

        public event ChatChangedEventHandler ChatChanged;

        public int Chrono
        {
            get { return chrono; }
        }

        public void Clear()
        {
            messages.Clear();
            FireChatChanged(this, new ChatChangedEventArgs(true, Chrono, Count));
        }

        public int Count
        {
            get { return messages.Count; }
        }

        public ChatItem[] GetLasts(int from)
        {
            if (from < 0 || from >= Count)
                return new ChatItem[0];

            return messages.GetRange(from, Count - from).ToArray();
        }

        public string GetNewUserID()
        {
            nbUser++;
            return UserName + nbUser;
        }

        public ChatItem[] History
        {
            get { return messages.ToArray(); }
        }

        public ChatItem this[int index]
        {
            get
            {
                if (index < 0 || index >= Count)
                    throw new ArgumentException("Index out of range.");
                else
                    return messages[index];
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            throw new NotImplementedException();
        }

        #endregion


        /************************************************************
         *                          Méthodes                        *
         ************************************************************/
        /// <summary>
        /// Déclenchement de l'évenement ChatChanged.
        /// </summary>
        /// <param name="sender"></param>Déclencheur de l'évenement.
        /// <param name="arg"></param>L'argument de l'évenement.
        public void FireChatChanged(Object sender, ChatChangedEventArgs arg)
        {
            if (ChatChanged != null)
                ChatChanged(this, arg);
        }

    }
}
